<template>
  <div class="modal-mask">
    <div class="modal-container">
      <!-- 顶部导航栏 -->
      <div class="modal-header">
        <button class="back-btn" @click="$emit('close')">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M19 12H5M12 19l-7-7 7-7"/>
          </svg>
        </button>
        <span class="header-title">第三步，选择医生</span>
        <button class="home-btn">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>
            <polyline points="9,22 9,12 15,12 15,22"/>
          </svg>
        </button>
      </div>

      <!-- 搜索内容区域 -->
      <div class="search-content">
        <div class="search-section">
          <h2 class="search-title">医生</h2>
          <p class="search-subtitle">找到您想看的医生</p>
          
          <div class="search-box">
            <svg class="search-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <circle cx="11" cy="11" r="8"/>
              <path d="m21 21-4.35-4.35"/>
            </svg>
            <input
              v-model="keyword"
              placeholder="输入医生姓名或科室"
              class="search-input"
              @focus="handleFocus"
            />
            <button v-if="keyword" class="clear-btn" @click="clearSearch">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <line x1="18" y1="6" x2="6" y2="18"/>
                <line x1="6" y1="6" x2="18" y2="18"/>
              </svg>
            </button>
          </div>
        </div>

        <!-- 搜索结果 -->
        <div class="search-results" v-if="keyword">
          <div v-if="filtered.length === 0" class="empty-state">
            <p>未找到相关医生</p>
          </div>
          <div v-else class="results-list">
            <div
              v-for="doctor in filtered"
              :key="doctor._id"
              @click="select(doctor)"
              class="result-item"
            >
              <div class="doctor-avatar">
                <img 
                  :src="doctor.avatar" 
                  :alt="doctor.name"
                  @error="handleImageError"
                />
              </div>
              <div class="doctor-info">
                <h3 class="doctor-name">{{ doctor.name }}</h3>
                <p class="doctor-department">{{ doctor.department }}</p>
                <div class="doctor-rating">
                  <svg width="14" height="14" viewBox="0 0 24 24" fill="#FFD700" stroke="#FFD700" stroke-width="2">
                    <polygon points="12,2 15.09,8.26 22,9.27 17,14.14 18.18,21.02 12,17.77 5.82,21.02 7,14.14 2,9.27 8.91,8.26"/>
                  </svg>
                  <span class="rating-score">{{ doctor.rating.toFixed(1) }}</span>
                  <span class="rating-count">({{ doctor.reviewCount }} 评价)</span>
                </div>
              </div>
              <svg class="arrow-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="9,18 15,12 9,6"/>
              </svg>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({ 
  doctors: Array,
  visible: Boolean
})
const emit = defineEmits(['select', 'close'])

const keyword = ref('')

const filtered = computed(() => {
  if (!keyword.value) return []
  return props.doctors.filter(doctor =>
    doctor.name.includes(keyword.value) || 
    doctor.department.includes(keyword.value)
  )
})

function select(doctor) {
  emit('select', doctor)
}

function clearSearch() {
  keyword.value = ''
}

function handleFocus() {
  // 处理搜索框聚焦事件
}

function handleImageError(event) {
  const img = event.target
  img.src = ''
}
</script>

<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9999;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.modal-container {
  background: #fff;
  width: 100%;
  max-width: 400px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 顶部导航栏 */
.modal-header {
  background: #1877F2;
  color: white;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
}

.back-btn,
.home-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-btn:hover,
.home-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.header-title {
  font-size: 18px;
  font-weight: 500;
  flex: 1;
  text-align: center;
  margin: 0 16px;
}

/* 搜索内容区域 */
.search-content {
  flex: 1;
  background: #f8f9fa;
  overflow-y: auto;
}

.search-section {
  background: white;
  padding: 24px 20px;
  border-radius: 0 0 16px 16px;
  margin-bottom: 8px;
}

.search-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.search-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0 0 24px 0;
}

/* 搜索框 */
.search-box {
  position: relative;
  display: flex;
  align-items: center;
  background: #f5f5f5;
  border-radius: 12px;
  padding: 12px 16px;
  border: 2px solid transparent;
  transition: all 0.2s ease;
}

.search-box:focus-within {
  border-color: #1877F2;
  background: white;
  box-shadow: 0 0 0 3px rgba(24, 119, 242, 0.1);
}

.search-icon {
  color: #999;
  margin-right: 12px;
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  border: none;
  background: none;
  outline: none;
  font-size: 16px;
  color: #333;
}

.search-input::placeholder {
  color: #999;
}

.clear-btn {
  background: none;
  border: none;
  color: #999;
  cursor: pointer;
  padding: 4px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 8px;
}

.clear-btn:hover {
  background: #f0f0f0;
}

/* 搜索结果 */
.search-results {
  background: white;
  margin: 0 16px;
  border-radius: 12px;
  overflow: hidden;
}

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: #999;
}

.results-list {
  max-height: 400px;
  overflow-y: auto;
}

.result-item {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.result-item:last-child {
  border-bottom: none;
}

.result-item:hover {
  background: #f8f9fa;
}

.doctor-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(135deg, #00D4FF 0%, #5B73FF 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-right: 12px;
}

.doctor-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.doctor-info {
  flex: 1;
}

.doctor-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 0 0 4px 0;
}

.doctor-department {
  font-size: 14px;
  color: #666;
  margin: 0 0 4px 0;
}

.doctor-rating {
  display: flex;
  align-items: center;
  gap: 4px;
}

.rating-score {
  font-size: 13px;
  font-weight: 600;
  color: #666;
}

.rating-count {
  font-size: 13px;
  color: #999;
}

.arrow-icon {
  color: #ccc;
  margin-left: 12px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .modal-container {
    max-width: 100%;
  }
  
  .search-section {
    padding: 20px 16px;
  }
  
  .search-results {
    margin: 0 12px;
  }
}
</style>